package com.buildings.dialog;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.util.CustomToast;
import com.buildings.util.NetUtils;
import com.buildings.util.Utils;
import com.buildings.view.NumberProgressBar;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by songwei on 2017/1/6.
 */

public class UpdateAppDialog extends Activity {

    private int W;
    private int H;



    private String serverVersion = "1.0"; //从服务器获取的版本号
//    private String updateDescription = "检测到新版本，立即更新吗?\n建议在wifi条件下更新"; //更新内容描述信息
    private String updateDescription ; //更新内容描述信息
    private boolean mForceUpdate = false; //是否强制更新


    private AlertDialog alertDialog1, alertDialog2; //表示提示对话框、进度条对话框

    @BindView(R.id.rl_content)
    RelativeLayout rlContent;
    @BindView(R.id.btn_update)
    Button btnUpdate;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_next_update)
    TextView tvNextUpdate;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_app_update);
        ButterKnife.bind(this);
        W = getResources().getDisplayMetrics().widthPixels;
        H = getResources().getDisplayMetrics().heightPixels;
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = (int) (W*0.79f);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;//设置对话框置顶显示
        win.setAttributes(lp);
        initView();
        initData();
    }

    private void initView(){
//        LinearLayout.LayoutParams rlContentLp = (LinearLayout.LayoutParams) rlContent.getLayoutParams();
        int paddingLeft = (int) (W*0.058);
        rlContent.setPadding(paddingLeft,0,paddingLeft, Utils.dp2px(this,30));
        RelativeLayout.LayoutParams btnUpdateLp = (RelativeLayout.LayoutParams) btnUpdate.getLayoutParams();
        btnUpdateLp.width = (int) (W*0.461);
        btnUpdateLp.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
    }

    private void initData(){
        serverVersion = getIntent().getStringExtra("version");
        updateDescription = getIntent().getStringExtra("content");
        mForceUpdate = getIntent().getBooleanExtra("force",false);
        if(mForceUpdate){
            tvNextUpdate.setVisibility(View.GONE);
        }else {
            tvNextUpdate.setVisibility(View.VISIBLE);
        }
        if(!TextUtils.isEmpty(updateDescription)){
            tvContent.setText(updateDescription);
        }
        if(!TextUtils.isEmpty(serverVersion)){
            tvVersion.setText("V"+serverVersion);
        }
    }

    @OnClick({R.id.btn_update,R.id.tv_next_update})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_update:
                if (!NetUtils.isNetworkconnected(this)) {
                    CustomToast.showToast(this, "网络连接异常,请检查网络设置!");
                    return;
                }
                setResult(RESULT_OK);
                finish();
                break;
            case R.id.tv_next_update:
                Intent intent = new Intent();
                intent.putExtra("from",1000);//暂不更新
                finish();
                break;
        }
    }






    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(mForceUpdate){
                //强制更新
                return false;
            }
        }
        return false;
//        return super.onKeyDown(keyCode, event);
    }
}
